package ar.com.ironsoft.javaopenauth.utils;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * A enum for the possible actions of each oauth service can do
 * 
 * @author Tomas de Priede
 * @version 0.1.2, 05/02/2011
 * @since 0.1.2-SNAPSHOT
 * 
 */
public enum ServletActionType {
	LOGIN("login"), CALLBACK("callback"), REFRESH("refresh");

	String actionName;

	private ServletActionType(String actionName) {
		this.actionName = actionName;
	}

	/**
	 * Returns the action name.
	 * 
	 * @return a String version of the actionName
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 */
	public String getActionName() {
		return actionName;
	}

	/**
	 * Builds a ServletActionType from a String.
	 * @param action the String action to convert
	 * @return a enum from a String 
	 * @author Tomas de Priede
	 * @version 0.1.2, 05/02/2011
	 * @since 0.1.2-SNAPSHOT
	 * @see ar.com.ironsoft.javaopenauth.utils.ValidatorUtils#validateNullOrEmpty(Object...)
	 */
	public static ServletActionType buildEnum(String action) {
		ValidatorUtils.validateNullOrEmpty(action);
		action = StringUtils.lowerCase(action);

		if (StringUtils.equalsIgnoreCase(LOGIN.getActionName(), action)) {
			return LOGIN;
		}
		if (StringUtils.equalsIgnoreCase(CALLBACK.getActionName(), action)) {
			return CALLBACK;
		}
		if (StringUtils.equalsIgnoreCase(REFRESH.getActionName(), action)) {
			return REFRESH;
		}
		return null;
	}
}
